package com.xq.mapper.impl;

import com.xq.mapper.UserMapper;
import com.xq.pojo.User;
import com.xq.utils.MyBatisUtils;
import com.xq.utils.PageUtils;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xq
 * @version 1.0
 * @date 2021/4/2 15:54
 * @description 用户数据接口实现层
 */
public class UserMapperImpl implements UserMapper {
    static SqlSession session = MyBatisUtils.openSession();
    @Override
    public int addUser(User user) {
        // 1、通过 MyBatisUtils 类的openSession()方法 创建 SqlSession 对象
        session = MyBatisUtils.openSession();
        // 2、通过 session 对象 操作数据库的CURD操作
        int state = session.insert("com.xq.mapper.UserMapper.addUser", user);
        // 3、提交事务
        session.commit();
        // 4、关闭session
        session.close();
        return state;
    }

    @Override
    public int updateUserById(User user) {
        session = MyBatisUtils.openSession();
        int state = session.update("com.xq.mapper.UserMapper.updateUserById", user);
        session.commit();
        session.close();
        return state;
    }

    @Override
    public int deleteUserByUserId(Integer userId) {
        session = MyBatisUtils.openSession();
        int state = session.delete("com.xq.mapper.UserMapper.deleteUserByUserId", userId);
        session.commit();
        session.close();
        return state;
    }

    @Override
    public List<User> getUserAll() {
        session = MyBatisUtils.openSession();
        List<User> list = session.selectList("com.xq.mapper.UserMapper.getUserAll");
        session.commit();
        session.close();
        return list;
    }

    @Override
    public User getUserOne(Integer userId) {
        session = MyBatisUtils.openSession();
        User u = session.selectOne("com.xq.mapper.UserMapper.getUserOne", userId);
        session.commit();
        session.close();
        return u;
    }

    @Override
    public List<User> getUserByFuzzyName(String name) {
        session = MyBatisUtils.openSession();
        List<User> list = session.selectList("com.xq.mapper.UserMapper.getUserByFuzzyName", name);
        session.commit();
        session.close();
        return list;
    }

    @Override
    public List<User> getUserByFuzzyUserInName(User user) {
        session = MyBatisUtils.openSession();
        List<User> list = session.selectList("com.xq.mapper.UserMapper.getUserByFuzzyUserInName", user);
        session.commit();
        session.close();
        return list;
    }

    @Override
    public List<User> getUserByPage(PageUtils page) {
        session = MyBatisUtils.openSession();
        // 查询总条数
        int totalDataCount = this.getUserByTotalDataCount();
        // 设置数据的总条数
        page.setTotalDataCount(totalDataCount);

        HashMap<String, Object> map = new HashMap<>(16);
        map.put("startIndex",(page.getCurrentPageNumber()-1)*page.getPageDataSize());
        map.put("pageDataSize",page.getPageDataSize());
        // 查询用户的分页数据
        List<User> list = session.selectList("com.xq.mapper.UserMapper.getUserByPage", map);

        session.close();
        return list;
    }

    @Override
    public Integer getUserByTotalDataCount() {
        session = MyBatisUtils.openSession();
        // 查询总条数
        int totalDataCount = session.selectOne("com.xq.mapper.UserMapper.getUserByTotalDataCount");
        return totalDataCount;
    }

    @Override
    public List<User> getUserByPageUseRowBounds(PageUtils pageUtils) {
        session = MyBatisUtils.openSession();
        // 设置总条数
        pageUtils.setTotalDataCount(this.getUserByTotalDataCount());
        RowBounds rb = new RowBounds(
                (pageUtils.getCurrentPageNumber() - 1)
                        * pageUtils.getPageDataSize()
                , pageUtils.getPageDataSize()
        );
        List<User> list = session.selectList("com.xq.mapper.UserMapper.getUserByPageUseRowBounds", null, rb);
        session.close();
        return list;
    }

    @Override
    public List<User> getAllUseMySQLAs() {
        session = MyBatisUtils.openSession();
        List<User> list = session.selectList("com.xq.mapper.UserMapper.getAllUseMySQLAs");
        session.close();
        return list;
    }

    @Override
    public List<User> getUserByPageInFuzzyUserNameAndUserAge(User user) {
        session = MyBatisUtils.openSession();
        // 设置总数据条数
        user.setTotalDataCount(this.getUserByTotalDataCount());
        // 创建一个map，进行存分页条件
        Map<String, Object> map = new HashMap<>(16);
        map.put("startIndex",(user.getCurrentPageNumber()-1)*user.getPageDataSize());
        map.put("pageDataSize",user.getPageDataSize());
        List<User> list = session.selectList("com.xq.mapper.UserMapper.getUserByPageInFuzzyUserNameAndUserAge", map);
        session.close();
        return list;
    }

    @Override
    public List<User> getUserByPageInFuzzyUserNameAndUserPasswordDefaultUserAge(User user) {
        session = MyBatisUtils.openSession();
        // 设置总数据条数
        user.setTotalDataCount(this.getUserByTotalDataCount());
        // 创建一个map，进行存分页条件
        Map<String, Object> map = new HashMap<>(16);
        map.put("startIndex",(user.getCurrentPageNumber()-1)*user.getPageDataSize());
        map.put("pageDataSize",user.getPageDataSize());
        map.put("userName",user.getUserName());
        map.put("userPassword",user.getUserPassword());
        map.put("userAge",user.getUserAge());
        List<User> list = session.selectList("com.xq.mapper.UserMapper.getUserByPageInFuzzyUserNameAndUserPasswordDefaultUserAge", map);
        session.close();
        return list;
    }

    @Override
    public int updateUserByIdInNullFields(User user) {
        session = MyBatisUtils.openSession();
        int state = session.update("com.xq.mapper.UserMapper.updateUserByIdInNullFields", user);
        session.commit();
        session.close();
        return state;
    }

    @Override
    public List<User> getUserByIds(List<Integer> ids) {
        session = MyBatisUtils.openSession();
        List<User> list = session.selectList("com.xq.mapper.UserMapper.getUserByIds",ids);
        session.close();
        return list;
    }
}
